﻿using System;
using System.ComponentModel;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Commands;
using StockholmsStad.Runtime.Serialization.DataContract.Enums;
using StockholmsStad.Runtime.Serialization.DataContract;

namespace StockholmsStad.Windows.UI.Modules.Couple.Models
{
    public class BookingPresentationModel : INotifyPropertyChanged
    {
        public event EventHandler<DataEventArgs<BookingPresentationModel>> Saved;
        public event EventHandler<DataEventArgs<BookingPresentationModel>> Cancelled;
        public DelegateCommand<object> CancelCommand { get; private set; }
        public DelegateCommand<object> SaveCommand { get; private set; }
        public DelegateCommand<object> UndoCommand { get; private set; }

        public BookingPresentationModel()
        {
            SaveCommand = new DelegateCommand<object>(Save);
            CancelCommand = new DelegateCommand<object>(Cancel);
            PropertyChanged += OnPropertyChangedEvent;
        }

        #region Properties

        public string HeaderInfo
        {
            get { return HeaderNames.Booking; }
        }

        public int? BookingId { get; set; }
        public int? TicketId { get; set; }

        public bool IsInChangeMode { get; set; }

        private TimeBlock bookedDate;
        public TimeBlock BookedDate
        {
            get
            {
                return bookedDate;
            }
            set
            {
                if (bookedDate == null || !bookedDate.Equals(value))
                {
                    bookedDate = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("BookedDate"));
                }
            }
        }

        private CeremonyType ceremonyType = CeremonyType.Individual;
        public CeremonyType CeremonyType
        {
            get
            {
                return ceremonyType;
            }
            set
            {
                if (!value.Equals(ceremonyType))
                {
                    ceremonyType = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("CeremonyType"));
                }
            }
        }

        private DateTime? preferredDate1;
        public DateTime? PreferredDate1
        {
            get
            {
                return preferredDate1;
            }
            set
            {
                if (!value.Equals(preferredDate1))
                {
                    preferredDate1 = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("PreferredDate1"));
                }
            }
        }

        private DateTime? preferredDate2;
        public DateTime? PreferredDate2
        {
            get
            {
                return preferredDate2;
            }
            set
            {
                if (!value.Equals(ceremonyType))
                {
                    preferredDate2 = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("PreferredDate2"));
                }
            }
        }

        private string ministerCode;
        public string MinisterCode
        {
            get
            {
                return ministerCode;
            }
            set
            {
                if (value == null || !value.Equals(ministerCode))
                {
                    ministerCode = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("MinisterCode"));
                }
            }
        }

        #endregion

        private void OnPropertyChangedEvent(object sender, PropertyChangedEventArgs e)
        {
            IsInChangeMode = true;

            SaveCommand.RaiseCanExecuteChanged();
            CancelCommand.RaiseCanExecuteChanged();
        }

        private void Save(object obj)
        {
            OnSaved(new DataEventArgs<BookingPresentationModel>(this));
        }

        private void Cancel(object obj)
        {
            OnCancel(new DataEventArgs<BookingPresentationModel>(this));
        }

        private void OnSaved(DataEventArgs<BookingPresentationModel> e)
        {
            var savedHandler = Saved;

            if (savedHandler != null)
                savedHandler(this, e);
        }

        private void OnCancel(DataEventArgs<BookingPresentationModel> e)
        {
            var cancelHandler = Cancelled;

            if (cancelHandler != null)
                cancelHandler(this, e);
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
